package com.atguigu.tingshu.user.strategy.impl;


import org.joda.time.DateTime;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.UpdateUserPaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
public class UserVipServiceStrategy implements UpdateUserPaidRecordStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper ;

    @Autowired
    private UserInfoMapper userInfoMapper ;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper ;

    @Transactional
    @Override
    public void updateUserPaidRecord(UserPaidRecordVo userPaidRecordVo) {

        // 保证接口的幂等性
        LambdaQueryWrapper<UserVipService> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserVipService::getOrderNo , userPaidRecordVo.getOrderNo()) ;
        UserVipService userVipService = userVipServiceMapper.selectOne(lambdaQueryWrapper);
        if(userVipService != null) {
            return ;
        }

        // 保存vip购买的交易信息
        userVipService = new UserVipService();
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        Long userId = userPaidRecordVo.getUserId();
        userVipService.setUserId(userId);

        // 查询用户数据
        UserInfo userInfo = userInfoMapper.selectById(userId);

        /**
         * 计算开始时间逻辑：
         * 1、判断当前用户是否为vip用户，如果是vip用户并且vip还没有过期，那么此时就把vip的过期时间作为开始时间。
         * 2、如果当前用户不是vip或者(是vip但是vip已经过期了)，那么就把当前的系统时间作为开始时间
         */
        Date startTime = new Date() ;
        if(userInfo.getIsVip().equals(1) && startTime.before(userInfo.getVipExpireTime())) {
            startTime = userInfo.getVipExpireTime();
        }
        userVipService.setStartTime(startTime);

        // 计算vip的过期时间
        Long vipServiceConfigId = userPaidRecordVo.getItemIdList().get(0);
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipServiceConfigId);

        /**
         * 关于日期相关计算的操作，不需要自己手动计算，可以直接使用日期工具包：
         * 1、joda-time
         * 2、hutool
         */
        Date expireTime = new DateTime(startTime).plusMonths(vipServiceConfig.getServiceMonth()).toDate();
        userVipService.setExpireTime(expireTime);
        userVipServiceMapper.insert(userVipService) ;

        // 修改用户表中的vip的过期时间
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(expireTime);
        userInfoMapper.updateById(userInfo) ;


    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_VIP;
    }

}